package com.bishe.tax.controller.sys;

import com.bishe.tax.entity.RegistrationInfo;
import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxRegistration;
import com.bishe.tax.service.RegistrationInfoService;
import com.bishe.tax.service.TaxRegistrationService;
import com.bishe.tax.service.ToDoaskService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.StringUtils;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 登记 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Controller
@RequestMapping("/system/info")
public class RegistrationInfoController {
    private String prefix = "registrationInfo";

    @Autowired
    private RegistrationInfoService infoService;
    @Autowired
    private TaxRegistrationService taxRegistrationService;
    @Autowired
    private ToDoaskService taskService;
    @Autowired
    RuntimeService runtimeservice;

    @GetMapping("list")
    public String info() {
        return prefix + "/list";
    }

    @GetMapping("infoList")
    public String infoList() {
        return prefix + "/trList";
    }

    /**
     * 查询登记列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RegistrationInfo info, @RequestParam(value = "fuye", required = false) String fuye,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotEmpty(fuye)) {
            info.setRiContinueTime(new Date());
        }
        List<RegistrationInfo> list = infoService.selectInfoList(info);
        return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
    }

    /**
     * 新增登记
     */
    @GetMapping("/add")
    public String add(@RequestParam("txId") String txId, ModelMap mmap) {
        TaxRegistration taxRegistration = taxRegistrationService.selectRegistrationById(Integer.valueOf(txId));
        mmap.put("taxRegistration", taxRegistration);
        return prefix + "/add";
    }

    /**
     * 新增保存登记
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RegistrationInfo info, @RequestParam("txId") String txId) {
        infoService.insertInfo(info);
        TaxRegistration registration = taxRegistrationService.selectRegistrationById(Integer.valueOf(txId));
        registration.setInfoId(info.getRiId());
        taxRegistrationService.updateRegistration(registration);
        return AjaxResult.success();
    }

    /**
     * 修改登记
     */
    @GetMapping("/edit/{riId}")
    public String edit(@PathVariable("riId") String riId,
                       @RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
        RegistrationInfo info = infoService.selectInfoById(Integer.valueOf(riId));
        mmap.put("registrationInfo", info);
        mmap.put("taskId", taskId);
        return prefix + "/edit";
    }

    /**
     * 修改保存登记
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RegistrationInfo info, @RequestParam(value = "taskId", required = false) String taskId) {
        if (StringUtils.isNotEmpty(taskId)) {
            taskService.doComplete(taskId, null);
            info.setStatus(2);
        }
        return AjaxResult.success(infoService.updateInfo(info));
    }

    /**
     * 删除登记
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String[] ar = ids.split(",");
        for (String s : ar) {
            RegistrationInfo registrationInfo = infoService.selectInfoById(Integer.valueOf(s));
            Integer taxId = registrationInfo.getTaxReId();
            TaxRegistration registration = taxRegistrationService.selectRegistrationById(taxId);
            registration.setInfoId(-1);
            taxRegistrationService.updateRegistration(registration);
            infoService.deleteInfoById(Integer.valueOf(s));
            String pid = registrationInfo.getProcessInstanceId();
            if (StringUtils.isNotBlank(pid)) {
                runtimeservice.deleteProcessInstance(registrationInfo.getProcessInstanceId(), "强行删除");
            }
        }
        return AjaxResult.success();
    }

    @GetMapping("examine/{taskId}/{riId}")
    public String doExamine(@PathVariable("riId") String riId, @PathVariable("taskId") String taskId, ModelMap mmap) {
        RegistrationInfo info = infoService.selectInfoById(Integer.valueOf(riId));
        mmap.put("registrationInfo", info);
        mmap.put("taskId", taskId);
        return prefix + "/registrationInfo";
    }

    /**
     * 审核
     *
     * @param taskId
     * @param flag
     */
    @PostMapping("complete")
    @ResponseBody
    public AjaxResult doComplete(RegistrationInfo info, @RequestParam("taskId") String taskId,
                                 @RequestParam("flag") Boolean flag) {
        taskService.doComplete(taskId, flag);
        if (flag) {
            info.setStatus(4);
        } else {
            info.setStatus(3);
        }
        return AjaxResult.success(infoService.updateInfo(info));
    }

    /**
     * 提交审核
     *
     * @return
     */
    @PostMapping("submitFlow")
    @ResponseBody
    public AjaxResult submitFlow(@RequestParam("riId") String riId, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        return AjaxResult.success(infoService.doSubmitFlow(user, riId));
    }


    /**
     * 手动复业
     *
     * @return
     */
    @PostMapping("startup")
    @ResponseBody
    public AjaxResult startup(@RequestParam("riId") String id) {
        RegistrationInfo registrationInfo = infoService.selectInfoById(Integer.valueOf(id));
        registrationInfo.setStatus(0);
        infoService.updateInfo(registrationInfo);

        TaxRegistration taxRegistration = taxRegistrationService.selectRegistrationById(registrationInfo.getTaxReId());
        taxRegistration.setInfoId(-1);
        taxRegistrationService.updateRegistration(taxRegistration);
        return AjaxResult.success();
    }
}
